1. Field of the Invention
The present invention relates to the design of virtual machines that execute platform-independent applications within computer systems. More specifically, the present invention relates to a method and an apparatus for reducing the overhead involved in executing native code methods from a platform-independent application running on a virtual machine.
2. Related Art
The rapid proliferation of the Internet has in part been fueled by the development of computer languages, such as the JAVA™ programming language distributed by Sun Microsystems, Inc. of Santa Clara, Calif. The Java programming language allows an application to be compiled into a module containing platform-independent byte codes, which can be distributed across a network of many different computer systems. Any computer system possessing a corresponding platform-independent virtual machine, such as the Java Virtual Machine, is then able to execute the byte codes. In this way, a single form of the application can be easily distributed to and executed by a large number of different computing platforms.
In some cases, it is useful for a platform-independent application to be able to access compiled code written in other languages. To this end, the Java Virtual Machine (JVM) provides the Java Native Interface (JNI), which enables Java applications to access native methods. Through such native methods, Java applications are able to perform low-level system operations, such as I/O operations.
Furthermore, JNI provides an interface through which native code can manipulate heap objects within the JVM in a platform-independent way. For example, native code may call the “GetObjectField” JNI function to fetch a value of a field in a Java heap object. In doing so, the native code does not have know how the Java object is represented in a specific JVM.
Although the JNI facilitates portability of native code across JVM implementations (on a given platform), every call to a native code method through the JNI involves time-consuming indirect calls and associated indirect references. Furthermore, every access to a heap object from the native code method also involves time-consuming indirect calls and associated indirect references. These indirect calls and associated indirect references can introduce a significant amount of overhead, especially for calls to methods that perform very little computational work.
Hence, what is needed is a method and an apparatus that reduces the overhead involved in calling a native code method from a platform-independent application.